<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 查询代理申请 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column prop="id" label="申请ID" width="55" align="center"></el-table-column>
                <el-table-column prop="nowlevel" label="申请人等级" align="center">
                    <template scope="scope">
                        <p v-if="scope.row.nowlevel == '1'">一级代理</p>
                        <p v-else-if="scope.row.nowlevel == '2'">二级代理</p>
                        <p v-else-if="scope.row.nowlevel == '3'">三级代理</p>
                        <p v-else-if="scope.row.nowlevel == '4'">普通会员</p>
                        <p v-else-if="scope.row.nowlevel > 4 || scope.row.nowlevel < 1">数据有误</p>
                    </template>
                </el-table-column>
                <el-table-column prop="levelapply" label="目标等级" align="center">
                    <template scope="scope">
                        <p v-if="scope.row.levelapply == '1'">一级代理</p>
                        <p v-else-if="scope.row.levelapply == '2'">二级代理</p>
                        <p v-else-if="scope.row.levelapply == '3'">三级代理</p>
                        <p v-else-if="scope.row.levelapply > 3 || scope.row.levelapply < 1">数据有误</p>
                    </template>
                </el-table-column>
                <el-table-column prop="describe" label="申请内容" align="center"></el-table-column>
                <el-table-column prop="status" label="当前处理状态" align="center">
                    <template slot-scope="scope">
                        <div v-if="scope.row.status == '0'">待处理</div>
                        <div v-if="scope.row.status == '1'">已同意</div>
                        <div v-if="scope.row.status == '2'">已拒绝</div>
                    </template>
                </el-table-column>
                <el-table-column prop="applicationtime" label="申请提交时间" align="center"></el-table-column>
                <el-table-column prop="disposetime" label="处理时间" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <div v-if="scope.row.status == '1'">已同意</div>
                        <div v-if="scope.row.status == '2'">已拒绝</div>
                        <div v-if="scope.row.status == '0'">
                            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                                >删除</el-button
                            >
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="40%">
            <el-form ref="nowform" :rules="rules" :model="nowform" label-width="100px">
                <el-form-item label="申请ID">
                    <div v-text="form.id"></div>
                </el-form-item>
                <el-form-item label="目标等级" prop="levelapply" label-width="100px">
                    <el-select v-model="nowform.levelapply" placeholder="请选择">
                        <el-option label="一级代理" value="1" :disabled="item.levleOne"></el-option>
                        <el-option label="二级代理" value="2" :disabled="item.levleTwo"></el-option>
                        <el-option label="三级代理" value="3" :disabled="item.levleThree"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="申请描述" prop="describe" label-width="100px">
                    <el-input type="textarea" rows="5" v-model="nowform.describe"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { selectMyLevelApplicationform, updateLevelApplicationform, deleteLevelApplicationform } from '../../api/testIndex.js';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                agentName: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {
                levelapply: '',
                describe: '',
                id: ''
            },
            nowform: {
                levelapply: '',
                describe: '',
                id: ''
            },
            mid: {},
            rules: {
                levelapply: [{ required: true, message: '请选择代理等级', trigger: 'change' }],
                describe: [
                    { required: true, message: '请填写描述', trigger: 'blur' },
                    { min: 10, message: '最少十个字符', trigger: 'blur' }
                ]
            },
            item: {
                levleOne: true,
                levleTwo: true,
                levleThree: true
            },
            idx: -1,
            id: -1
        };
    },
    created() {
        this.selectMyLevelApplicationform();
    },
    methods: {
        // 获取后端传送的数据数据
        selectMyLevelApplicationform() {
            selectMyLevelApplicationform(this.query).then((res) => {
                if (res.code == 403) {
                    this.$router.push('/403');
                }
                if (res.code == 200) {
                    this.tableData = res.t.list;
                    this.pageTotal = res.t.total;
                    console.log(this.tableData);
                }
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    console.log(row.id);
                    this.mid.id = row.id;
                    deleteLevelApplicationform(this.mid).then((res) => {
                        console.log(res);
                        if (res == '成功') {
                            this.$message.success('删除成功');
                            this.tableData.splice(index, 1);
                        }
                        if (res == '失败') {
                            this.$message.error('失败');
                        }
                    });
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }

            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
            this.nowform.describe=row.describe;
            if (row.nowlevel == 2) {
                this.item.levleOne = false;
            } else if (row.nowlevel == 3) {
                this.item.levleOne = false;
                this.item.levleTwo = false;
            } else if (row.nowlevel == 4) {
                this.item.levleOne = false;
                this.item.levleThree = false;
            }
        },
        // 保存编辑
        saveEdit() {
            {
                (this.nowform.id = this.form.id),
                    updateLevelApplicationform(this.nowform).then((res) => {
                        console.log(res);
                        if (res == '成功') {
                            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                            this.editVisible = false;
                            this.$set(this.tableData, this.idx, this.form);
                            this.selectMyLevelApplicationform();
                        } else {
                            this.$message.error(`修改第 ${this.idx + 1} 行失败`);
                        }
                    });
            }
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.selectMyLevelApplicationform();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
